/**
 * @author:albert
 * @date:2016年6月7日
 */
package com.bkbw.live.battle.business.behaviors;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.badlogic.gdx.math.Bezier;
import com.badlogic.gdx.math.Vector3;
import com.bkbw.live.common.GameConfig;

/**
 * @author albert
 *
 */
public class BezierMove3 {
	
	Logger logger = LoggerFactory.getLogger(GameConfig.LoggerName);
	public static int Angle30 = 30;
	public static int Angle60 = 60;
	public static float BezierHeight = 5f;
	
	private Vector3 start = new  Vector3();
	private Vector3 end = new  Vector3();
	private int angle;
	private float radians;
	private Bezier<Vector3> bezier;
	private Vector3 controlVector = new Vector3();
	private float speedLen;
	private float bezierLen;
	private float currentLen;
	private float height;
	private Vector3 controlVector2 = new Vector3();
	
	public BezierMove3(Vector3 start, Vector3 end, float height, float speedLen) {
		reset(start, end, height, speedLen);
	}
	
	public BezierMove3(Vector3 start, Vector3 end, int angle, float speedLen) {
		reset(start, end, angle, speedLen);
	}
	
	public void reset(Vector3 start, Vector3 end, float height, float speedLen) {
		this.start.set(start);
		this.end.set(end);
		this.height = height;
		this.speedLen = speedLen;
		controlVector.set(this.start).add(0, this.height, 0);
		controlVector2.set(this.end).add(0, this.height, 0);
		
		Vector3[] va = new Vector3[4];
		va[0] = new Vector3(this.start);
		va[1] = new Vector3(this.controlVector);
		va[2] = new Vector3(this.controlVector2);
		va[3] = new Vector3(this.end);
		bezier = new Bezier<Vector3>(va);
		bezierLen = bezier.approxLength(5);
	}
	
	public void reset(Vector3 start, Vector3 end, int angle, float speedLen) {
		this.start.set(start);
		this.end.set(end);
		Vector3 sub = new Vector3();
		this.angle = angle;
		radians = angle * GameConfig.Degree1;
		System.out.println("radians " + radians);
		this.speedLen = speedLen;
		float controlVectorLen = sub.set(this.end).sub(this.start).len() / 2 / (float)Math.cos(radians);
		
		Vector3 yAxis = new Vector3(0, 1, 0);
		// 3d贝塞尔和2d贝塞尔计算方式几乎一样，只是旋转是沿着一条垂直的轴来旋转，需要先求出这条轴的向量
		Vector3 rotAxis = new Vector3();
		Vector3 path = new Vector3();
		path.set(this.end).sub(this.start);
		System.out.println("path "+path);
		rotAxis.set(this.end).sub(this.start).rotate(yAxis, -90);
		System.out.println("rotAxis "+rotAxis);
		controlVector.set(this.end).sub(this.start).rotate(rotAxis, angle).nor().scl(controlVectorLen).add(this.start);
		
		System.out.println("controlVector " + controlVector);
		
		Vector3[] va = new Vector3[3];
		va[0] = new Vector3(this.start);
		va[1] = new Vector3(this.controlVector);
		va[2] = new Vector3(this.end);
		bezier = new Bezier<Vector3>(va);
		bezierLen = bezier.approxLength(5);
	}
	
	public void getFirst(Vector3 pos, Vector3 derivate) {
		bezier.valueAt(pos, 0.0f);
		bezier.derivativeAt(derivate, 0.0f);
	}
	
	/*
	 * pos 位置
	 * derivate 子弹朝向
	 * @return true移动结束，false还有后续数据
	 */
	public boolean move(float dt, Vector3 pos, Vector3 derivate) {
		boolean moveEnd = false;
		float movement = dt * speedLen;
		currentLen += movement;
		if (currentLen >= bezierLen) {
			currentLen = bezierLen;
			moveEnd = true;
		}
		float t = currentLen / bezierLen;
		bezier.valueAt(pos, t);
		bezier.derivativeAt(derivate, t);
		return moveEnd;
	}
	
	public void changeSpeed(int speedLen) {
		this.speedLen = speedLen;
	}
	
	public float getRate() {
		return currentLen / bezierLen;
	}
}
